package com.xcjw.com.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xcjw.com.Activity.CarActivity;
import com.xcjw.com.Activity.CenterActivity;
import com.xcjw.com.Activity.LoginActivity;
import com.xcjw.com.Activity.WeekFirstActivity;
import com.xcjw.com.Base.Myapplication;
import com.xcjw.com.Bean.LoginUser;
import com.xcjw.com.Hy.DemoHelper;
import com.xcjw.com.MainActivity;
import com.xcjw.com.R;
import com.xcjw.com.Utils.DensityUtil;
import com.xcjw.com.Utils.FileUtils;
import com.xcjw.com.View.CustomDialog;
import com.xcjw.com.View.SimpleTool;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @packname com.xcjw.com.Fragment
 * @filename HomeFragment
 * @date on 2018/11/19 11:46
 *****/
public class HomeFragment extends BaseFragment implements View.OnClickListener {

      @BindView(R.id.big_bg)
      RelativeLayout bigBg;
      @BindView(R.id.view1)
      ImageView view1;
      @BindView(R.id.text1)
      TextView text1;
      @BindView(R.id.zhongxin_layout)
      RelativeLayout zhongxinLayout;
      @BindView(R.id.view2)
      ImageView view2;
      @BindView(R.id.text2)
      TextView text2;
      @BindView(R.id.mouth_layout)
      RelativeLayout mouthLayout;
      @BindView(R.id.view3)
      ImageView view3;
      @BindView(R.id.text3)
      TextView text3;
      @BindView(R.id.week_layout)
      RelativeLayout weekLayout;
      @BindView(R.id.view4)
      ImageView view4;
      @BindView(R.id.text4)
      TextView text4;
      @BindView(R.id.huiyishi)
      RelativeLayout huiyishi;
      @BindView(R.id.view5)
      ImageView view5;
      @BindView(R.id.text5)
      TextView text5;
      @BindView(R.id.jaiban)
      RelativeLayout jaiban;
      @BindView(R.id.view6)
      ImageView view6;
      @BindView(R.id.text6)
      TextView text6;
      @BindView(R.id.car_layout)
      LinearLayout carLayout;
      @BindView(R.id.txt_left_title)
      TextView txtLeftTitle;
      @BindView(R.id.txt_main_title)
      TextView txtMainTitle;
      @BindView(R.id.txt_right_titleS)
      TextView txtRightTitleS;
      @BindView(R.id.txt_right_title)
      TextView txtRightTitle;
      @BindView(R.id.simple_toolbar)
      SimpleTool simpleToolbar;
      Unbinder unbinder;
      @BindView(R.id.HUIYI)
      CardView HUIYI;
      @BindView(R.id.jiaban)
      CardView jiaban;
      @BindView(R.id.yongche)
      CardView yongche;
      private LoginUser.ContentBean user;
      private CustomDialog dialog;

      @Override
      protected void onLazyLoad() {

      }

      @Override
      protected void initview() {

      }

      @Override
      public void init() {

      }

      @Override
      public int getlayoutId() {
            return R.layout.home_fragment;
      }

      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // TODO: inflate a fragment view
            View rootView = super.onCreateView(inflater, container, savedInstanceState);
            unbinder = ButterKnife.bind(this, rootView);
            float dimension = getResources().getDimension(R.dimen.x200);
            int i = (int) dimension;
            int dip2px = DensityUtil.dip2px(30);
            int dip2px1 = DensityUtil.dip2px(4);
//            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) HUIYI.getLayoutParams();
//            layoutParams.setMargins(dip2px, -(i + dip2px1), dip2px, 0);
            user = (LoginUser.ContentBean) FileUtils.getObject(mActivity, "loginUser");
//            if (user.getIsShow() == 0) {
//                  HUIYI.setVisibility(View.GONE);
//                  jiaban.setVisibility(View.GONE);
//                  yongche.setVisibility(View.GONE);
//            } else {
//                  HUIYI.setVisibility(View.VISIBLE);
//                  jiaban.setVisibility(View.VISIBLE);
//                  yongche.setVisibility(View.VISIBLE);
//            }
            txtRightTitle.setOnClickListener(this);
            if (TextUtils.isEmpty(user.getNeighborhoodCommittees())) {
                  txtRightTitle.setText("");
                  //txtRightTitle1.setText("");
            } else {
                  txtRightTitle.setText(user.getNeighborhoodCommittees());
                  //txtRightTitle1.setText(user.getNeighborhoodCommittees());
            }


            //HUIYI.setLayoutParams(layoutParams);
            zhongxinLayout.setOnClickListener(this);
            mouthLayout.setOnClickListener(this);
            huiyishi.setOnClickListener(this);
            jaiban.setOnClickListener(this);
            carLayout.setOnClickListener(this);
            weekLayout.setOnClickListener(this);
            return rootView;
      }

      @Override
      public void onDestroyView() {
            super.onDestroyView();
            unbinder.unbind();

      }

      @Override
      public void onClick(View v) {
            switch (v.getId()) {
                  case R.id.txt_right_title:
                        showiosDialog();

                        break;
                  case R.id.zhongxin_layout:
                        startActivity(new Intent(mActivity, CenterActivity.class));
                        break;
                  case R.id.mouth_layout:
                        Intent intent4 = new Intent(mActivity, WeekFirstActivity.class);
                        intent4.setFlags(0);
                        startActivity(intent4);
                        break;
                  case R.id.week_layout:
                        Intent intent3 = new Intent(mActivity, WeekFirstActivity.class);
                        intent3.setFlags(1);
                        startActivity(intent3);
                        break;
                  case R.id.huiyishi:
                        Intent intent = new Intent(mActivity, CarActivity.class);
                        intent.setFlags(1);
                        startActivity(intent);
                        break;
                  case R.id.jaiban:
                        Intent intent1 = new Intent(mActivity, CarActivity.class);
                        intent1.setFlags(2);
                        startActivity(intent1);
                        break;
                  case R.id.car_layout:
                        Intent intent2 = new Intent(mActivity, CarActivity.class);
                        intent2.setFlags(3);
                        startActivity(intent2);
                        break;
            }
      }

      private void showiosDialog() {
            dialog = new CustomDialog(mActivity, R.style.customDialog, R.layout.ios_dialog2);
            dialog.show();
            TextView tvContent = (TextView) dialog.findViewById(R.id.textsign);
            tvContent.setText("确定要退出登录吗？");
            dialog.setCanceledOnTouchOutside(false);
            TextView tvOk = (TextView) dialog.findViewById(R.id.tv_bind);
            TextView tvcancle = (TextView) dialog.findViewById(R.id.tv_cancl);
            tvOk.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                        Myapplication.editor.putBoolean("isLogin", false).commit();
                        FileUtils.saveObject(mActivity, "User", "");
                        FileUtils.saveObject(mActivity, "loginUser", "");
                        ((MainActivity) mActivity).finish();
                        startActivity(new Intent(mActivity, LoginActivity.class));
                        DemoHelper.getInstance().logout(false, null);
                        dialog.dismiss();
                  }
            });
            tvcancle.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                        dialog.dismiss();
                  }
            });

      }
}